home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / dev / c / RConfig.lha / RConfig_v1.1 / rlib / rlib_setjmp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-13  |  4.0 KB  |  176 lines

  1. /*
  2.  * rlib.c   v1.3
  3.  * ~~~~~~
  4.  *   Copyright (C) 1986,1987 by Manx Software Systems, Inc.
  5.  *   Copyright (C) 1992 by Anthon Pang, Omni Communications Products.
  6.  *
  7.  *   Replacement library for:
  8.  *     - setjmp/longjmp.a68
  9.  */
  10.  
  11. /*
  12.  * setjmp()/longjmp()
  13.  */
  14. #ifdef __SETJMP_REPLACE
  15.  
  16. #if !defined(__ALLOCA_REPLACE) && !defined(__DYNASTACK_STKCHK)
  17. #error "Missing alloca() and/or stkchk() for replacement setjmp()"
  18. #endif
  19.  
  20. #asm
  21.     cseg
  22.  
  23.     public  _setjmp
  24. _setjmp
  25.     move.l  (sp)+,d0            ; save return address
  26.     move.l  (sp),a0             ; get ptr to env_buf
  27.     movem.l d0-d7/a1-a7,(a0)    ; save PC and regs d1-d7/a1-a7
  28. #endasm
  29.  
  30. #if defined(__ALLOCA_REPLACE) || defined(__DYNASTACK_STKCHK)
  31. #asm
  32.     lea     60(a0),a0
  33. #endasm
  34. #endif
  35.  
  36. #ifdef __ALLOCA_REPLACE
  37. #asm
  38.     move.l  __last_alloca_blk#,(a0)+
  39. #endasm
  40. #endif
  41. #ifdef __DYNASTACK_STKCHK
  42. #asm
  43.     move.l  __stkbase#,(a0)+
  44. #endasm
  45. #endif
  46.  
  47. #asm
  48.     move.l  d0,a0               ; put PC in a0
  49.     move.l  #0,d0               ; return
  50.     jmp     (a0)                ; continue
  51.  
  52.  
  53. SETUPOLDSTACK   macro
  54.     move.l  56(a0),a6           ; get a7 from jmp_buf
  55.     move.l  (a0),-(a6)          ; push PC onto old stack
  56.     move.l  52(a0),-(a6)        ; push a6 onto old stack
  57.   endm
  58.  
  59. COPYTOOLDSTACK  macro
  60.     sub.l   #68,sp              ; to avoid overwriting current stack frame
  61.  
  62.     move.l  a4,-(sp)            ; save a4 (for near data references)
  63.     movem.l 4(a0),d1-d7/a1-a5   ; get regs from jmp_buf
  64.     movem.l d0-d7/a1-a5,-(a6)   ; save regs on old stack
  65. #endasm
  66.  
  67. #if defined(__ALLOCA_REPLACE) && defined(__DYNASTACK_STKCHK)
  68. #asm
  69.     move.l  64(a0),-(a6)        ; copy __stkbase to old stack
  70. #endasm
  71. #endif
  72.  
  73. #asm
  74.     move.l  60(a0),-(a6)        ; copy __last_alloca_blk (or __stkbase)
  75.  
  76.     move.l  (sp)+,a4            ; restore a4
  77.     move.l  a6,a7               ; swap stacks!
  78.   endm
  79.  
  80. JMPTOOLDSTACK   macro
  81.     movem.l (sp)+,d0-d7/a1-a6   ; restore regs
  82.     move.l  (sp)+,a0            ; restore old PC
  83.     jmp     (a0)
  84.   endm
  85.  
  86.  
  87.     xref    _free
  88.  
  89.     public  _longjmp
  90. _longjmp
  91.     addq.l  #4,sp
  92.     move.l  (sp)+,a0
  93. #endasm
  94.  
  95. #if defined(__ALLOCA_REPLACE) || defined(__DYNASTACK_STKCHK)
  96. #asm
  97.     SETUPOLDSTACK
  98. #endasm
  99. #endif
  100.  
  101. #asm
  102.   IF INT32
  103.     move.l  (sp),d0
  104.   ELSE
  105.     move.w  (sp),d0
  106.   ENDC
  107.     bne     9$
  108.     move.l  #1,d0               ; force to 1 to avoid conflict with setjmp()
  109. 9$
  110. #endasm
  111.  
  112. #if !defined(__ALLOCA_REPLACE) && !defined(__DYNASTACK_STKCHK)
  113. #asm
  114.     movem.l 4(a0),d1-d7/a1-a7   ; restore regs
  115.     move.l  (a0),a0             ; get return address
  116.     jmp (a0)
  117. #endasm
  118. #endif
  119.  
  120. #if defined(__ALLOCA_REPLACE) || defined(__DYNASTACK_STKCHK)
  121. #asm
  122.     COPYTOOLDSTACK
  123. #endasm
  124. #endif
  125.  
  126. #ifdef __ALLOCA_REPLACE
  127. #asm
  128.     move.l  (sp)+,a3                ; get saved __last_alloca_blk
  129.     move.l  __last_alloca_blk,a2    ; get current pointer
  130.  
  131.     move.l  a3,__last_alloca_blk    ; restore __last_alloca_blk
  132.  
  133. next_alloca_blk:
  134.     cmpa.l  a2,a3                   ; have we popped sub-chain yet?
  135.     beq     last_alloca_blk
  136.  
  137.     move.l  a2,-(sp)                ; push pointer for free()ing
  138.     move.l  4(a2),a2                ; get previous __last_alloca_blk
  139.  
  140.     jsr     _free                   ; free block
  141.     addq.l  #4,sp                   ; clean up
  142.     bra     next_alloca_blk
  143.  
  144. last_alloca_blk:
  145. #endasm
  146. #endif
  147.  
  148. #ifdef __DYNASTACK_STKCHK
  149. #asm
  150.     move.l  (sp)+,a3                ; get saved __stkbase
  151.     move.l  __stkbase,a2            ; get current pointer
  152.     move.l  a3,__stkbase            ; restore __stkbase
  153.  
  154. next_stkbase:
  155.     cmpa.l  a2,a3                   ; have we popped sub-chain yet?
  156.     beq     last_stkbase
  157.  
  158.     move.l  a2,-(sp)                ; push pointer for free() call
  159.     add.l   -(a2),a2                ; add block size to get top of ext. stack
  160.     move.l  (a2),a2                 ; get previous __stkbase
  161.     jsr     _free                   ; free block
  162.     addq.l  #4,sp                   ; clean up
  163.     bra     next_stkbase
  164.  
  165. last_stkbase
  166. #endasm
  167. #endif
  168.  
  169. #if defined(__ALLOCA_REPLACE) || defined(__DYNASTACK_STKCHK)
  170. #asm
  171.     JMPTOOLDSTACK
  172. #endasm
  173. #endif
  174.  
  175. #endif /* __SETJMP_REPLACE */
  176.